let baseUrl = 'https://houchang-alpha.comeonline.cn/api/v2/' // alpha环境
// let baseUrl = 'https://houchang-stg.comeonline.cn/api/v2/' // stg环境
// let baseUrl = 'https://houchang.comeonline.cn/api/v2/' // 正式环境
// let baseUrl = 'http://ddns.zapbird.com:8888/api/v2/'
// let baseUrl = 'http://ddns.comeonline.cn:8888/api/v2/'

let userMessage = '';
if(uni.getStorageSync('userMessage')){
	userMessage = uni.getStorageSync('userMessage').data

}
const request = (params) => {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: '加载中',
			mask:true
		});
		uni.request({
			method: params.method,
			url: baseUrl + params.url,
			data: params.data,
			header: {
				// "Authorization": userMessage.token,
				"Authorization":uni.getStorageSync('token')
				// "Content-Type": params.contentType || "application/x-www-form-urlencoded"
			},
			dataType: 'json',
		}).then((response) => {
			uni.hideLoading();
			uni.stopPullDownRefresh();
			let [error, res] = response;
			if (res.data.code == 0) {
				console.log(params.url+'请求参数：',params.data);
				console.log(params.url+'请求响应：',res.data);
				resolve(res.data);
			}else if(res.data.status == 401){ // 未登录状态
				const token = uni.getStorageSync('token');
				if(!token){
					uni.showToast({
						title: '请先登录',
						icon: 'none',
						mask:true,
						duration: 2000,
						complete(res) {
							setTimeout(function(){
								uni.reLaunch({
									url: '/subcontractList/login/index/index'
								});
							},2000);

						}
					});
				}
			}else if(res.data.status == -101){  // code获取访问用户身份是否已绑定状态
				resolve(res.data);
			}else {
				console.log('222',res);
				reject(res.data)
				uni.showToast({
					title: res.data.message,
					icon: 'none',
					duration: 2000
				});
			}
		}).catch(error => {
			uni.hideLoading();
			uni.stopPullDownRefresh();
			// let [err, res] = error;
			// reject(error)
			console.log('后端返回错误信息：',error);
			console.log('后端返回错误data信息：',error.data);

			reject(error.data)
			uni.showToast({
				title: error.data.message,
				icon: 'none',
				duration: 2000
			});

		})
	});
}
export default request


module.exports = {
	baseUrl,
	request
}
